package com.contract.management.util;

import java.util.ArrayList;
import java.util.List;

import com.contract.management.domain.CommJpaRepository;
import com.contract.management.domain.Contract;
import com.contract.management.domain.WorkflowNode;

public class WorkFlowUtils {
	
	public static WorkflowNode getNextNode(Contract ct, CommJpaRepository repo) {
		WorkflowNode nextNode = null;
		int curNodeId = ct.getCurNode().getId();
		switch (curNodeId) {
		case 1:	//合同申请、修改
		case 2:
			if(ct.getHasFjtk()) {
				nextNode = getNext4(ct, repo);
			} else {
				nextNode = getNext4Or6(ct, repo);
			}
			break;
		case 3:	//法务部初审,下一步生成合同
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 6);
			break;
		case 4:	//副总初审,下一步生成合同或总经理审核，
			//附加条款法务初审或总经理初审
			if(ct.getHasFjtk()) {
				if(ct.getAudit() != null && ct.getAudit().getDept().getRight().getId() == 4) {
					nextNode = repo.queryOne("from WorkflowNode where id = ?0", 3);
				} else {
					nextNode = repo.queryOne("from WorkflowNode where id = ?0", 5);
				}				
				nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0 or u.dept.right.id = ?1", nextNode.getRight().getId(), 4));
			} else {
				nextNode = getNext5Or6(ct, repo);
			}
			
			break;
		case 5:	//总经理初审,下一步法务初审或生成合同
			if(ct.getHasFjtk()) {
				nextNode = repo.queryOne("from WorkflowNode where id = ?0", 3);
				nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
			} else {
				nextNode = repo.queryOne("from WorkflowNode where id = ?0", 6);
			}
			break;
		case 6:	//打印合同,下一步合同盖章
			nextNode = getNext7(ct, repo);
			break;
		case 7:	//合同盖章，下一步上传合同资料
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 8);
			break;
		case 8: //上传资料，下一步法务部复审
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 9);
			nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
			break;
		case 9:	//下一步选择节点
		case 10:
		case 11:
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 15);
		case 12:
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 13);
			break;
		case 16:
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 12);
			nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
		default:
			break;
		}
		return nextNode;
	}

	
	private static WorkflowNode getNext4(Contract ct, CommJpaRepository repo) {
		WorkflowNode nextNode = repo.queryOne("from WorkflowNode where id = ?0", 4);
		nextNode.setDesc("合同有附加条款，需要副总经理审核");
		nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
		return nextNode;
	}
	//判断下一步是否副总审核或打印合同
	private static WorkflowNode getNext4Or6(Contract ct, CommJpaRepository repo) {
		WorkflowNode nextNode = null;
		double fybl = (double)ct.getFyhj() / (double)ct.getZcjg();
		String fyblStr = String.format("%.0f", fybl * 100);
		double fzbl = getFzbl(repo);
		double zjlbl = getZjlbl(repo);
		if(fybl <= fzbl) {
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 4);
			if(fybl <= zjlbl) {
				nextNode.setDesc("合同金额小于正常价格" + fyblStr + "%，需要副总和总经理审核");	
			} else {
				nextNode.setDesc("合同金额小于正常价格" + fyblStr + "%，需要副总审核");
			}			
			nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
		} else {
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 6);
		}
		return nextNode;
	}
	//判断下一步是否总经理审核或打印合同
	private static WorkflowNode getNext5Or6(Contract ct, CommJpaRepository repo) {
		WorkflowNode nextNode = null;
		
		double fybl = (double)ct.getFyhj() / (double)ct.getZcjg();
		String fyblStr = String.format("%.0f", fybl * 100);
		//总经理审核比例
		double zjlbl = getZjlbl(repo);
		if(fybl <= zjlbl) {
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 5);
			nextNode.setDesc("合同金额小于正常价格" + fyblStr + "%，需要总经理审核");
			nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
		} else {
			nextNode = repo.queryOne("from WorkflowNode where id = ?0", 6);
		}
		return nextNode;
	}

	private static WorkflowNode getNext7(Contract ct, CommJpaRepository repo) {
		WorkflowNode nextNode = repo.queryOne("from WorkflowNode where id = ?0", 7);
		nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
		return nextNode;
	}
	private static WorkflowNode getNext9(Contract ct, CommJpaRepository repo) {
		WorkflowNode nextNode = repo.queryOne("from WorkflowNode where id = ?0", 9);
		nextNode.setOpers(repo.query("select new User(id, name) from User u where u.dept.right.id = ?0", nextNode.getRight().getId()));
		return nextNode;
	}
	private static WorkflowNode getNext12Or10Or8(Contract ct, CommJpaRepository repo) {
		return null;
	}
	/**
	 * 获得副总比例
	 * @return
	 */
	private static double getFzbl(CommJpaRepository repo) {
		String fzsh = repo.queryOne("select value from SysConfig where key = ?0", "FZSH");
		return Double.parseDouble(fzsh.replace("%", "")) * 0.01;
	}
	/**
	 * 获得总经理比例
	 * @return
	 */
	private static double getZjlbl(CommJpaRepository repo) {
		String zjlsh = repo.queryOne("select value from SysConfig where key = ?0", "ZJLSH");
		return Double.parseDouble(zjlsh.replace("%", "")) * 0.01;
	}
	
	/*
	private static final List<WorkflowNode> WORKFLOWNODES = new ArrayList<WorkflowNode>() {{
		add(new WorkflowNode(1,	"合同录入",		0));
		add(new WorkflowNode(2, "合同修改",		0));
		add(new WorkflowNode(3, "法务部初审",	4));
		add(new WorkflowNode(4, "副总初审",		2));
		add(new WorkflowNode(5, "总经理初审",	1));
		add(new WorkflowNode(6, "打印合同",		0));
		add(new WorkflowNode(7, "合同盖章",		3));
		add(new WorkflowNode(8, "上传合同资料",	0));
		add(new WorkflowNode(9, "法务部复审",	4));
		add(new WorkflowNode(10,"副总复审",		2));
		add(new WorkflowNode(11,"总经理复审",	1));
		add(new WorkflowNode(12,"流程部复核",	3));
		add(new WorkflowNode(13,"审核完成",		0));
		add(new WorkflowNode(14,"审核终止",		0));
		add(new WorkflowNode(15,"选择节点",		0));
	}};
	
	private static WorkflowNode getNode(int id) {
		for (WorkflowNode node : WORKFLOWNODES) {
			if(node.getId() == id) return node;
		}
		return null;
	}
	*/
}
